package exhi.action;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stp.core.Constants;
import com.stp.core.entity.IGridRowFactory;
import com.stp.core.entity.XGrid;
import com.stp.core.entity.XGridPage;
import com.stp.core.entity.XGridRow;
import com.stp.core.entity.XMsg;
import com.stp.core.spring3.BaseSpringAction;
import com.stp.rbac.entity.Dept;
import com.stp.rbac.manager.DeptMng;
import com.stp.std.entity.LogInfo;

@Controller
@RequestMapping("/entp")
public class EntpLogAct extends BaseAct {

	private static final String PATH_TABLE = "/log-table";
	private static final String PATH_LIST = "/log-list";

	private String group = "";

	// 信息列表
	@RequestMapping(PATH_TABLE)
	public void table(
			@RequestParam(value = "group", required = true, defaultValue = "") String group,
			ModelMap model) {
		model.addAttribute("group", group);
	}

	// 权限json数据
	@RequestMapping(PATH_LIST)
	@ResponseBody
	public String list(
			XGridPage page,
			@RequestParam(value = "group", required = true, defaultValue = "") String group)
			throws InstantiationException, IllegalAccessException {
		this.group = group;
		String sql = "from LogInfo where userId='" + getEntpUser().getUid()
				+ "'";
		sql += " order by logTime desc";
		LogGridRow row = new LogGridRow();
		XGrid grid = logMng.getXGrid(sql, row, page.getPageNo(),
				page.getPageSize(), false);
		if (null == grid)
			return "";
		return "{\"xGrid\":" + toJson(grid) + "}";
	}

	class LogGridRow implements IGridRowFactory {
		public XGridRow builderXRow(Object obj) {
			XGridRow xrow = null;
			LogInfo logInfo;
			if (obj instanceof LogInfo) {
				xrow = new XGridRow();
				LogInfo info = (LogInfo) obj;
				xrow.setId(info.getId().toString());
				List t = new ArrayList();				
				t.add(info.getId().toString());
				t.add(EntpLogAct.this.getEntpUser().getMc());
				SimpleDateFormat df = new SimpleDateFormat(
						Constants.DEF_DATEFORMAT_LONG);
				t.add(df.format(info.getLogTime()));
				t.add(info.getIp());
				if (!"01".equalsIgnoreCase(EntpLogAct.this.group))
					t.add(info.getLogGroup());
				t.add(info.getContent());
				xrow.setData(t);
			}
			return xrow;
		}
	}
}
